import pygame
from random import choice, randint
from src.player import Alien
from src.enemies import Animal, Snake, Frog, Snail, LadyBug, Bat, Bee, Fly
from src.bullet import Bullet
from src.background import show_background
from src.collision import is_player_collide_enemy, get_bullet_collide_enemy
from src.board import Title
from src.music import play_background_music, play_explosion_sound, play_shoot_sound



class Game:
    def __init__(self):  # 类初始化
        pygame.init()
        self.screen = pygame.display.set_mode((1000, 800))
        self.clock = pygame.time.Clock()
        self.running = True
        self.game_active = True
        self.game_score = 0

        self.cooldown_time = 1
        self.cooldown_speed = 0.05  # 调整子弹发射间隔
        self.is_ready_to_shoot = True

        self.player_group = pygame.sprite.GroupSingle()
        self.player_group.add(Alien())

        self.enemies_group = pygame.sprite.Group()

        self.bullet_group = pygame.sprite.Group()

        # 触发定时器
        self.enemy_emerge_event = pygame.USEREVENT + 1
        pygame.time.set_timer(self.enemy_emerge_event, randint(1, 5) * 1000)

    def run(self):
        play_background_music()
        while self.running:
            show_background(self.screen)
            
            score_str = f'分数：{self.game_score}'
            score_board = Title(score_str, 30, 'red', (10, 10))
            score_board.draw(self.screen)

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.running = False

                if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                    self.game_active = True

                if self.game_active:
                    if event.type == self.enemy_emerge_event:
                        animal = choice((Snake, Frog, Snail, LadyBug, Bat, Fly, Bee))  # choice()方法从元组、列表等中随机选择一个元素
                        self.enemies_group.add(animal())

                    if event.type == pygame.MOUSEBUTTONDOWN:                    
                        if self.is_ready_to_shoot:
                            bullet = Bullet(self.player_group.sprite.rect.midtop)
                            play_shoot_sound()
                            self.bullet_group.add(bullet)
                            self.is_ready_to_shoot = False

                    if not self.is_ready_to_shoot:
                        self.cooldown_time -= self.cooldown_speed
                        if self.cooldown_time <= 0:
                            self.cooldown_time = 1
                            self.is_ready_to_shoot = True

            if self.game_active:
                
                self.player_group.draw(self.screen)
                self.player_group.update()

                self.enemies_group.draw(self.screen)
                self.enemies_group.update(self.player_group.sprite.rect.center)

                self.bullet_group.draw(self.screen)
                self.bullet_group.update()

                if enemy := is_player_collide_enemy(self.player_group.sprite, self.enemies_group):
                    if not enemy[0].is_dead:
                        self.game_active = False

                if bullet_and_enemy := get_bullet_collide_enemy(self.bullet_group, self.enemies_group):
                    bullet: Bullet = bullet_and_enemy[0]
                    dead_enemy: Animal = bullet_and_enemy[1]
                    if not dead_enemy.is_dead:
                        bullet.kill()
                        play_explosion_sound()
                        dead_enemy.die()
                        self.game_score += dead_enemy.SCORE

            else:
                title = Title("GAME OVER", 120, "BLACK", (180, 200))
                title.draw(self.screen)
                self.enemies_group.empty()
                self.bullet_group.empty()
                self.player_group.sprite.rect.x = 90

            pygame.display.flip()
    
    def quit(self):
        pygame.quit()
